package com.fr.report.js;

import com.fr.base.FCloneable;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.web.Repository;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FormSubmitJavaScript extends AbstractJavaScript
  implements CallBackable
{
  private String action = "";
  private boolean asynchronous = true;
  private JavaScript callBack;

  public String getAction()
  {
    return this.action;
  }

  public void setAction(String paramString)
  {
    this.action = paramString;
  }

  public void setCallBack(JavaScript paramJavaScript)
  {
    this.callBack = paramJavaScript;
  }

  public JavaScript getCallBack()
  {
    return this.callBack;
  }

  public boolean isAsynchronous()
  {
    return this.asynchronous;
  }

  public void setAsynchronous(boolean paramBoolean)
  {
    this.asynchronous = paramBoolean;
  }

  public String createJS(Repository paramRepository)
  {
    Map localMap = paramRepository.checkoutTplContext();
    Parameter[] arrayOfParameter = getParameters();
    JSONObject localJSONObject = new JSONObject();
    for (int i = 0; i < arrayOfParameter.length; ++i)
      try
      {
        localJSONObject.put(arrayOfParameter[i].getName(), arrayOfParameter[i].getValue());
      }
      catch (JSONException localJSONException)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      }
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append("var config={");
    localStringBuffer.append("url:\"" + this.action + "\",");
    localStringBuffer.append("asyn:" + this.asynchronous + ",");
    localStringBuffer.append("data:" + localJSONObject.toString() + ",");
    localStringBuffer.append("callback: function(){" + ((this.callBack != null) ? this.callBack.createJS(paramRepository) : "") + "}");
    localStringBuffer.append("};");
    localStringBuffer.append("this.options.form.formSubmit(config);");
    String str = localStringBuffer.toString();
    try
    {
      return TemplateUtils.render(str, localMap);
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return str;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str2;
      String str1 = paramXMLableReader.getTagName();
      if (str1.equals("Action"))
      {
        str2 = null;
        if ((str2 = paramXMLableReader.getElementValue()) != null)
          setAction(str2);
      }
      else if (str1.equals("Asyn"))
      {
        if (paramXMLableReader.getElementValue() != null)
          setAsynchronous(false);
      }
      else if (str1.equals("CallBack"))
      {
        str2 = null;
        if ((str2 = paramXMLableReader.getElementValue()) != null)
          setCallBack(new JavaScriptImpl(str2));
      }
      else if ("JavaScript".equals(str1))
      {
        setCallBack(ReportXMLUtils.readJavaScript(paramXMLableReader));
      }
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (!(StringUtils.isEmpty(this.action)))
      paramXMLPrintWriter.startTAG("Action").textNode(this.action).end();
    if (!(this.asynchronous))
      paramXMLPrintWriter.startTAG("Asyn").textNode(String.valueOf(this.asynchronous)).end();
    if (getCallBack() != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, getCallBack(), "JavaScript");
  }

  public boolean equals(Object paramObject)
  {
    return ((super.equals(paramObject)) && (paramObject instanceof FormSubmitJavaScript) && (ComparatorUtils.equals(((FormSubmitJavaScript)paramObject).action, this.action)) && (((FormSubmitJavaScript)paramObject).asynchronous == this.asynchronous));
  }

  public Object clone()
    throws CloneNotSupportedException
  {
    FormSubmitJavaScript localFormSubmitJavaScript = (FormSubmitJavaScript)super.clone();
    if (this.callBack != null)
      localFormSubmitJavaScript.callBack = ((JavaScript)this.callBack.clone());
    return localFormSubmitJavaScript;
  }
}